from collections import OrderedDict

from rest_framework.generics import ListAPIView
from rest_framework.response import Response

from apps.meiduo_admin.serializers.user import UserSerializer
from apps.users.models import User
from rest_framework.pagination import PageNumberPagination

# class PageNum(PageNumberPagination):
#     page_size = 5
#     page_size_query_param='pagesize'
#     max_page_size = 20
#
#     def get_paginated_response(self, data):
#         return Response(OrderedDict([
#             ('count', self.page.paginator.count),
#             ('lists', data),
#             ('page', self.page.number),
#             ('pages', self.page.paginator.num_pages),
#             ('pagesize', self.page.paginator.per_page),
#         ]))
from apps.meiduo_admin.utils import PageNum
from rest_framework.generics import ListCreateAPIView


class UserAPIView(ListCreateAPIView):
    # queryset = User.objects.all()
    serializer_class =UserSerializer
    pagination_class =PageNum
    def get_queryset(self):
        keyword=self.request.query_params.get('keyword')
        if keyword:
            return User.objects.filter(username__contains=keyword)
        return User.objects.all()